package v1

import (
	"github.com/icetech233/gin"
	"github.com/icetech233/gin/binding"
	"net/http"
	"strconv"
	"strings"
	"wms/pb"
	"wms/service/impl"
	"wms/service/request"
	"wms/service/response"
)

func (a *stockApi) SkuList(c *gin.Context) {
	queryParams := c.Request.URL.Query()
	arg := new(request.QueryStockSkuListRequest)
	stockStatus := queryParams.Get("stock_status")
	if stockStatus == "1" {
		arg.StockStatus = &isIn
	} else if stockStatus == "2" {
		arg.StockStatus = &isOut
	}
	d, err := impl.StockSrvImpl.SkuList(c, arg)
	if err != nil {
		response.Fail(-1, err.Error(), arg, c)
		return
	}
	response.OkWithData(d, c)
}

func (a *stockApi) InAddApply(c *gin.Context) {
	param := new(request.AddStockInApplyRequest)
	err := c.ShouldBindJSON(param)
	if err != nil {
		response.Fail(-1, "入参错误:"+err.Error(), param, c)
		return
	}
	err = impl.StockSrvImpl.InAddApply(c, param)
	if err != nil {
		response.Fail(-1, err.Error(), param, c)
		return
	}
	response.Ok(c)
}

func (a *stockApi) OutAddApply(c *gin.Context) {
	param := new(request.AddStockOutApplyRequest)
	err := c.ShouldBindJSON(param)
	if err != nil {
		response.Fail(-1, "入参错误:"+err.Error(), param, c)
		return
	}
	err = impl.StockSrvImpl.OutAddApply(c, param)
	if err != nil {
		response.Fail(-1, err.Error(), param, c)
		return
	}
	response.Ok(c)
}

func (a *stockApi) InAudit(c *gin.Context) {
	param := new(request.AuditStockInRequest)
	err := c.ShouldBindJSON(param)
	if err != nil {
		response.Fail(-1, "入参错误:"+err.Error(), param, c)
		return
	}
	err = impl.StockSrvImpl.InAudit(c, param)
	if err != nil {
		response.Fail(-1, err.Error(), param, c)
		return
	}
	response.Ok(c)
}

func (a *stockApi) OutAudit(c *gin.Context) {
	param := new(request.AuditStockOutRequest)
	err := c.ShouldBindJSON(param)
	if err != nil {
		response.Fail(-1, "入参错误:"+err.Error(), param, c)
		return
	}
	err = impl.StockSrvImpl.OutAudit(c, param)
	if err != nil {
		response.Fail(-1, err.Error(), param, c)
		return
	}
	response.Ok(c)
}

func (a *stockApi) InDetail(c *gin.Context) {
	queryParams := c.Request.URL.Query()
	stockInIdString := queryParams.Get("stock_in_id")
	stockInId, _ := strconv.ParseInt(stockInIdString, 10, 0)
	arg := new(request.QueryStockInRequest)
	arg.StockInID = &stockInId
	d, err := impl.StockSrvImpl.InDetail(c, arg)
	if err != nil {
		response.Fail(-1, err.Error(), arg, c)
		return
	}

	contentType := c.Request.Header.Get("Content-Type")
	isProtobuf := strings.Contains(contentType, binding.MIMEPROTOBUF)
	if isProtobuf {
		ret := new(pb.ResponseInDetail)
		ret.Code = pb.ReplyCodeType_Success
		ret.Msg = "success"
		ret.Data = d
		c.ProtoBuf(http.StatusOK, ret)
		return
	}

	response.OkWithData(d, c)
}

func (a *stockApi) InList(c *gin.Context) {
	queryParams := c.Request.URL.Query()
	arg := new(request.QueryStockInRequest)
	auditStatus := queryParams.Get("audit_status")
	if auditStatus == auth {
		arg.IsAuth = &isFilter
	}
	d, err := impl.StockSrvImpl.InList(c, arg)
	if err != nil {
		response.Fail(-1, err.Error(), arg, c)
		return
	}
	response.OkWithData(d, c)
}

func (a *stockApi) Statistics(c *gin.Context) {
	arg := new(request.QueryStockStatisticsRequest)
	d, err := impl.StockSrvImpl.Statistics(c, arg)
	if err != nil {
		response.Fail(-1, err.Error(), arg, c)
		return
	}
	response.OkWithData(d, c)
}

func (a *stockApi) OutList(c *gin.Context) {
	queryParams := c.Request.URL.Query()
	arg := new(request.QueryStockOutRequest)
	auditStatus := queryParams.Get("audit_status")
	if auditStatus == auth {
		arg.IsAuth = &isFilter
	}
	d, err := impl.StockSrvImpl.OutList(c, arg)
	if err != nil {
		response.Fail(-1, err.Error(), arg, c)
		return
	}
	response.OkWithData(d, c)
}

func (a *stockApi) OutDetail(c *gin.Context) {
	queryParams := c.Request.URL.Query()
	stockInIdString := queryParams.Get("stock_out_id")
	stockOutId, _ := strconv.ParseInt(stockInIdString, 10, 0)
	arg := new(request.QueryStockOutRequest)
	arg.StockOutID = &stockOutId
	d, err := impl.StockSrvImpl.OutDetail(c, arg)
	if err != nil {
		response.Fail(-1, err.Error(), arg, c)
		return
	}
	response.OkWithData(d, c)
}
